<template>
  <ex_super_dialog_form ref="reportDialog" v-bind="report_dialog" v-on="$listeners">
  </ex_super_dialog_form>
</template>
<script>
import role_system_mixin from "@/mixin/role_system_mixin";

import anomaly_desc_select from "@/component/common/anomaly_desc_select.vue";
export default {
  components: { anomaly_desc_select },
  mixins: [role_system_mixin],

  props: {
    orderTypeList: {
      type: Array,
      default: () => [],
    },
    extraModel: {
      type: Array,
      default: () => [],
    },
    formDisabled: {
      type: [Array, Boolean],
      default: false,
    },
    formType: {
      type: [Array, Boolean],
      default: false,
    },
    formHidden: {
      type: [Array, Boolean],
      default: false,
    },
  },
  data() {
    return {
      inFormDisabled: this.formDisabled,

      orders: null,

      processNodeList: [],
      report_dialog: {
        customClass: "dialog_auto",
        submitDataHandle: (data) => {
          const { attachedList, ...other } = data;
          if (attachedList) {
            if (this.isMainSystem || this.isWmsSystem) {
              other.anomalyAttachedList = attachedList;
            } else if (this.isTmsSystem) {
              other.attachedDTOList = attachedList;
            }
          }
          return other;
        },

        form: {
          itemClass: "form_item_default",
          inputClass: "form_input_default",
          formClass: "form_default_col2 flex_row_wrap",
          default: { anomalyOccurrenceDate: new Date().getTime() },
          model: [
            { header: "label_base_info" },
            {
              key: "customerId",
              label: "label_customer",
              if: () => !this.checkinFormHidden("customerId"),
              render: (h, { data }) => {
                if (this.checkinFormDisabled("customerId"))
                  return this.$formatNameCode(
                    data.customerName,
                    data.customerCode
                  );
                return h("common_customer_choose", {
                  class: "form_input_default",
                  props: {
                    value: data.customerId,
                    disabled: this.checkinFormDisabled("customerId"),
                  },
                  on: {
                    changeItem: (e) => {
                      this.$set(data, "customerId", e.customerId);
                      this.$set(data, "customerCode", e.customerCode);
                      this.$set(data, "customerName", e.customerName);
                    },
                  },
                });
              },
            },
            ...this.extraModel,
            {
              key: "orderType",
              label: "label_order_type",
              type: "select",
              if: () => !this.checkinFormHidden("orderType"),

              disabled: () => this.checkinFormDisabled("orderType"),

              formType: () => this.checkinFormType("orderType"),
              formatter: (r, c, v, i) =>
                this.$enumShow(v, "AbnormalOrderTypeEnum"),
              attrs: {
                enum: "AbnormalOrderTypeEnum",
                listHandler: (list) => {
                  if (this.orderTypeList.length > 0) return this.orderTypeList;
                  return list;
                },
              },
            },
            {
              key: "orderNo",
              label: "label_order_code",
              if: () => !this.checkinFormHidden("orderNo"),
              disabled: () => this.checkinFormDisabled("orderNo"),
              formType: () => this.checkinFormType("orderNo"),
              attrs: { maxlength: 30, "show-word-limit": true },
            },


            {
              key: "anomalyDescribeId",
              label: "label_anomaly_desc",
              render: (h, { data }) => {
                return h(anomaly_desc_select, {
                  class: "form_input_default",
                  props: {
                    classify: data.anomalyClassifyId,
                    describe: data.anomalyDescribeId,
                    classifyName: data.anomalyClassifyName,
                    describeName: data.anomalyDescribeName,
                    processNodeList: this.processNodeList,
                  },
                  on: {
                    classifyItem: (e) => {
                      if (!e) e = { id: null, name: null }
                      this.$set(data, "anomalyClassifyId", e.id);
                      this.$set(data, "anomalyClassifyName", e.name);
                    },
                    describeItem: (e) => {
                      if (!e) e = { id: null, name: null }
                      this.$set(data, "anomalyDescribeId", e.id);
                      this.$set(data, "anomalyDescribeName", e.name);
                    },
                  },
                });
              },
            },
            // {
            //   key: "anomalyCause",
            //   label: "label_reason_abnormal",
            //   type: "select",
            //   attrs: { enum: "AnomalyCauseEnum" },
            // },
            {
              key: "anomalyOccurrenceDate",
              label: "label_abnormal_happen_time",
              type: "date-picker",
              attrs: { clearable: false, type: "datetime", format: 'yyyy-MM-dd HH:mm'},
            },
            {
              key: "anomalyBoxQty",
              label: "label_abnormal_box_count",
              attrs: { maxlength: 9, "show-word-limit": true },
            },
            {
              key: "anomalySkuQty",
              label: "label_abnormal_piece_count",
              attrs: { maxlength: 9, "show-word-limit": true },
            },
            {
              key: "anomalySkuValue",
              label: "label_abnormal_sku_value",
              attrs: { maxlength: 9, "show-word-limit": true },
            },
            {
              key: "anomalyRemark",
              label: "label_abnormal_remark",
              attrs: { maxlength: 50, "show-word-limit": true },
            },
            {
              key: "attachedList",
              label: "label_abnormal_attachment",
              type: "upload-file",
              attrs: { limit: 3 },
              render: (h, { data }) => {
                return h("ex-upload-file", {
                  props: {
                    limit: 3,
                    value: data.attachedList,
                  },
                  on: {
                    input: (list) => {
                      this.$set(
                        data,
                        "attachedList",
                        list.map((it) => {
                          it.type = "ABNORMAL";
                          return it;
                        })
                      );
                    },
                  },
                });
              },
            },
          ],
          rules: {
            customerId: {
              required: true,
              message: this.$tt("tip_can_no_empty"),
              trigger: "blur",
            },
            anomalyDescribeId: {
              required: true,
              message: this.$tt("tip_can_no_empty"),
              trigger: "blur",
            },
            orderType: {
              required: true,
              message: this.$tt("tip_can_no_empty"),
              trigger: "blur",
            },
            orderNo: {
              required: true,
              message: this.$tt("tip_can_no_empty"),
              trigger: "blur",
            },
            anomalyBoxQty: {
              required: true,
              trigger: "blur",
              validator: (r, v, cb) => {
                if (_.isEmpty(v)) {
                  cb(this.$tt("tip_can_no_empty"));
                } else if (!this.$reg.PureNumber(v)) {
                  cb(this.$tt("tip_only_positive_pure_decimal"));
                } else {
                  cb();
                }
              },
            },
            anomalySkuQty: {
              required: true,
              trigger: "blur",
              validator: (r, v, cb) => {
                if (_.isEmpty(v)) {
                  cb(this.$tt("tip_can_no_empty"));
                } else if (!this.$reg.PureNumber(v)) {
                  cb(this.$tt("tip_only_positive_pure_decimal"));
                } else {
                  cb();
                }
              },
            },


            anomalyOccurrenceDate: {
              required: true,
              message: this.$tt("tip_can_no_empty"),
              trigger: "blur",
            },
          },
        },
      },
    };
  },

  methods: {
    checkinFormHidden(key) {
      if (this.formHidden) {
        return this.formHidden.some(it => it == key);
      }
      return false
    },
    checkinFormType(key) {
      if (this.formType) {
        return this.formType.some(it => it == key) ? 'text' : false;
      }
      return false;
    },
    checkinFormDisabled(key) {
      if (this.inFormDisabled) {
        if (this.inFormDisabled instanceof Array) {
          return this.inFormDisabled.some(it => it == key)
        }
        return this.inFormDisabled;
      }
      return false;
    },
    showNew(obj) {
      if (obj.formDisabled != undefined) this.inFormDisabled = obj.formDisabled;

      if (this.isWmsSystem) {
        this.processNodeList = ["COMMON"];
      } else {
        this.processNodeList = ["PICK_UP", "DELIVERY", "COMMON"];
      }
      const option = Object.assign(
        {},
        {
          title: this.$tt("label_tms_waybill_submit_exception"),
          submitUrl: this.isMainSystem
            ? this.$url["oms/BaseAnomalyReport"] :
            this.isWmsSystem ? this.$url["wms/BaseAnomalyReport"] : this.$url["tms/BaseAnomalyReport"],
          detailUrl: this.isMainSystem
            ? this.$url["oms/BaseAnomalyDetail"]
            : this.$url["wms/BaseAnomalyDetail"],
        },
        obj
      );
      this["$refs"]["reportDialog"].showNew(option);
    },
    // showNew(obj) {
    //   this['$refs']['reportDialog'].showNew({
    //     // submitUrl: this.$url['oms/BaseAnomalyEdit'],
    //     // params: { "id": row["id"] },
    //     title: this.$tt('label_tms_waybill_submit_exception')
    //   })
    // }
  },
};
</script>
